<!DOCTYPE html>
<html lang="cn">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>生成二维码页面</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
    integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
  <style>
    body {
      padding: 20px;
    }
  </style>
</head>

<body>
  <div class="container">
    <div class="row">
      <div class="col-md-6">
        <textarea class="form-control" id="inputText" rows="5" placeholder="输入要生成二维码的文本"></textarea>
      </div>
    </div>
    <div class="row mt-3">
      <div class="col-md-3">
        <input type="number" class="form-control" id="widthInput" placeholder="二维码宽度" value="500">
      </div>
      <div class="col-md-3">
        <input type="number" class="form-control" id="heightInput" placeholder="二维码高度" value="500">
      </div>
      <div class="col-md-3">
        <button class="btn btn-primary" onclick="generateQRCode()">生成二维码</button>
      </div>
    </div>
    <div class="row mt-3">
      <div class="col-md-12">
        <div id="qrcodeDiv"></div>
      </div>
    </div>
  </div>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.qrcode/1.0/jquery.qrcode.min.js"></script>
  <script>
    function generateQRCode() {
      var text = $("#inputText").val();
      var width = $("#widthInput").val();
      var height = $("#heightInput").val();

      if (text === '') {
        alert('请输入要生成二维码的文本');
        return;
      }

      if (width === '' || height === '') {
        width = 500;
        height = 500;
      }

    $("#qrcodeDiv").html('');

    // 对文本进行编码处理
    var encodedText = convertUtf16ToUtf8(text);

    $("#qrcodeDiv").qrcode({
        text: encodedText,
        width: width,
        height: height,
        correctLevel: 0
    });
    }

  function convertUtf16ToUtf8(str) {
    var out, i, len, c;
    out = "";
    len = str.length;
    for (i = 0; i < len; i++) {
      c = str.charCodeAt(i);
      if ((c >= 0x0001) && (c <= 0x007F)) {
        out += str.charAt(i);
      } else if (c > 0x07FF) {
        out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
        out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
        out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
      } else {
        out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
        out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
      }
    }
    return out;
  }

  </script>
</body>

</html>